//
// Created by ct on 2020/12/1.
//

#include "controller.h"

#include "nettop_run.h"

#include "peer_log_parser.h"

namespace controller {
    // 获取监控节点流量的信息
    std::string net_top(const url_parse::url_result_map& map){

//----------------------------------------------------------------------------------------------------------
//            std::chrono::system_clock::time_point latest_time = std::chrono::system_clock::now();
//            nettop::proc_mgr p_mgr;
//            nettop::proc_mgr::stats mgr_st;
//            nettop::ps_vec p_vec;
//            sleep(1);
//            const std::chrono::system_clock::time_point cur_time = std::chrono::system_clock::now();
//            std::list<nettop::packet_stats> ps_list;
//            nettop::nettop_mgr::Instance().p_list.swap(ps_list);
//            mgr_st.total_pkts = nettop::nettop_mgr::Instance().p_list.total_pkts.exchange(0);
//            p_mgr.bind_packets(ps_list, nettop::nettop_mgr::Instance().lam, p_vec, mgr_st,
//                               nettop::nettop_mgr::Instance().log_list);
//            // redraw 函数 内部有一个打印函数，这里有需要的信息
//            //  nettop::curses_setup::redraw(cur_time - latest_time, p_vec, mgr_st);
//            auto json_str = nettop::curses_setup::format_proc(cur_time - latest_time, p_vec, mgr_st);
//----------------------------------------------------------------------------------------------------------
        std::chrono::system_clock::time_point latest_time = std::chrono::system_clock::now();
        nettop::proc_mgr p_mgr;
        nettop::proc_mgr::stats mgr_st;
        nettop::ps_vec p_vec;
        sleep(1);
        const std::chrono::system_clock::time_point cur_time = std::chrono::system_clock::now();
        std::list<nettop::packet_stats> ps_list;
        nettop::nettop_mgr::Instance().p_list.swap(ps_list);
        mgr_st.total_pkts = nettop::nettop_mgr::Instance().p_list.total_pkts.exchange(0);
        p_mgr.bind_packets(ps_list, nettop::nettop_mgr::Instance().lam, p_vec, mgr_st,
                               nettop::nettop_mgr::Instance().log_list);
        auto json_str = nettop::curses_setup::format_proc(cur_time - latest_time, p_vec, mgr_st);
        return json_str;
    }
    // 呐！测试
    std::string hello(const url_parse::url_result_map& map){

        return "{\"Hello\":\""+map.at("router")+"\"}";
    }

    std::string peer_log_parser_handler(const url_parse::url_result_map& map){
        peer_log_parser::log_parser log_p(
                "/home/ct/workspace/log/peer0.org1.cn.log",
                "ALL",
                100,
                "ERRO");
        return log_p.result();
    }
}